package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPreview

import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperPreviewInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.RecyclerViewScrollHelper
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.ielts.aiysTrainPaper.aiysAnalysisPaper.AibkPreviewAnswerSheetParentAdapter
import com.lancoo.znbkxx.ielts.aiysTrainPaper.aiysAnalysisPaper.AibkPreviewAnswerSheetParentAdapter.onClickParentListener
import com.lancoo.znbkxx.teacher.R
import kotlinx.android.synthetic.main.aibk_dialog_paper_preview_answersheet.*


/**
 * auther: 王国龙
 * Date：
 * describe:
 */
class AibkPreviewAnswerSheetDialog(
        context: Context?,
        val activity: AppCompatActivity,
        val paperPreviewInfor: PaperPreviewInfor,
        val typeListPosition:Int
) : Dialog(context!!, R.style.CustomDialog) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.aibk_dialog_paper_preview_answersheet)

        setDialogTheme()

        //按空白处不能取消动画
        setCanceledOnTouchOutside(true)
        //初始化界面数据
        LlWrongBottomControl.visibility = View.GONE
        refreshView()
    }

    private fun setDialogTheme() {
        //设置布局顶部显示
        window!!.setGravity(Gravity.TOP)
        //设置背景透明后设置该属性，可去除dialog边框
        window!!.setBackgroundDrawable(ColorDrawable())
        //设置进入动画
        window!!.setWindowAnimations(R.style.MyDialogBottomTranslate)

        // 关键代码
        if (window != null) {
//            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
            //透明状态栏
            window!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
//            window!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window!!.getDecorView().setPadding(0, 0, 0, 0)
            window!!.getDecorView().setBackgroundColor(Color.WHITE)
            val layoutParams: WindowManager.LayoutParams = window!!.getAttributes()
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                // 延伸显示区域到刘海
                val lp: WindowManager.LayoutParams = window!!.getAttributes()
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                window!!.setAttributes(lp)
                // 设置页面全屏显示
                val decorView: View = window!!.getDecorView()
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            }
            window!!.setAttributes(layoutParams)
        }


    }

    /**
     * 初始化界面控件的显示数据
     */
    private fun refreshView() {

        RvAnswerSheetParent.recycledViewPool.clear()
        RvAnswerSheetParent.setItemViewCacheSize(0)
        val manager = LinearLayoutManager(context)
        manager.orientation = LinearLayoutManager.VERTICAL
        RvAnswerSheetParent.layoutManager = manager

        val adapterParent = AibkPreviewAnswerSheetParentAdapter(
                activity, paperPreviewInfor!!
        )
        RvAnswerSheetParent.adapter = adapterParent
        adapterParent.setClickParentListener(object : onClickParentListener {
            override fun onClick(
                    index_A: Int,
                    index_B: Int,
                    index_C: Int
            ) {
                var SortIndex = paperPreviewInfor!!.TypeList[index_A]
                                .QuesList[index_B].SortIndexList[index_C].SortIndex

                clickDialogListener!!.onSortIndexClick(SortIndex)

            }

        })


        SBtnBack.setOnClickListener {
            if (clickDialogListener != null) {
                clickDialogListener!!.onDismissClick()
            }
        }

        RecyclerViewScrollHelper.scrollToPosition(RvAnswerSheetParent, typeListPosition)

    }


    private var clickDialogListener: onClickDialogListener? = null

    fun setClickDialogListener(clickDialogListener: onClickDialogListener?) {
        this.clickDialogListener = clickDialogListener
    }

    /*
   * 设置答题卡题号点击事件
   * */
    interface onClickDialogListener {
        fun onSortIndexClick(sortIndex: String)
        fun onDismissClick()//点击提交按钮
    }

}